//
//  MainViewController.m
//  DriverBus
//
//  Created by MacMini20170218 on 2017/3/27.
//  Copyright © 2017年 JiYang. All rights reserved.
//

#import "MainViewController.h"
#import "CalcuBusDriection.h"

@interface MainViewController ()

@end

@implementation MainViewController{
    BMKLocationService *_locService;
    CLLocation *newLocation;
    GCDSocketManager *socketManager;
    AppDelegate *appDelegate;
    NSInteger set;
    NSInteger direction;
    NSTimer *mTimer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];    
    self.driver = appDelegate.driver;
    NSString *driverName = [NSString stringWithFormat:@"%@师傅",self.driver.name];
    NSString *driverId = [NSString stringWithFormat:@"工号:%@",self.driver.number];
    [self.driverNameLabel setText:driverName];
    [self.busTypeLabel setText:driverId];
    self.topInfoView.layer.borderWidth = 1;
    self.topInfoView.layer.cornerRadius = 1;
    self.topInfoView.layer.borderColor = [UIColor grayColor].CGColor;
    self.addPeople.layer.cornerRadius = 10;
    self.addPeople.layer.borderWidth = 1;
    self.addPeople.layer.borderColor = [UIColor orangeColor].CGColor;
    self.reducePeople.layer.cornerRadius = 10;
    self.reducePeople.layer.borderWidth = 1;
    self.reducePeople.layer.borderColor = [UIColor orangeColor].CGColor;
    self.peopleNumber.layer.cornerRadius = 60;
    self.peopleNumber.layer.masksToBounds = YES;
    self.terminalButton.layer.cornerRadius = 10;
    self.terminalButton.layer.borderWidth = 1;
    self.terminalButton.layer.borderColor = [UIColor orangeColor].CGColor;
    
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    
    socketManager = [GCDSocketManager sharedSocketManager];
    AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
    [netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
            {
                UIAlertView *alertNet = [[UIAlertView alloc]initWithTitle:@"网络错误" message:@"网络未连接，请检查网络连接." delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                alertNet.alertViewStyle = UIAlertViewStyleDefault;
                [alertNet show];
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"wwan");
                //启动LocationService
                [_locService startUserLocationService];
                [socketManager connectToSocket];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"wifi");
                //启动LocationService
                [_locService startUserLocationService];
                [socketManager connectToSocket];
                break;
            default:
                break;
        }
    }];
    [netManager startMonitoring];
}

- (IBAction)terminalButton:(id)sender {
    UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"到达终点?" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    aler.tag = 1;
    [aler show];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (alertView.tag) {
        case 1:
            //到达
            if(buttonIndex==0){ //确定按钮
                [socketManager cutOffSocket];
                [_locService stopUserLocationService];
                if([mTimer isValid]){
                    [mTimer invalidate];
                    mTimer = nil;
                }
                //协议方法，返回上一界面
                [self.delegate terminal];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            break;
        default:
            break;
    }
}

- (void)viewDidDisappear:(BOOL)animated{
}

#pragma mark 定位成功
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    if(!appDelegate.hasConnectServer){
        return;
    }
    newLocation = userLocation.location;
    if(mTimer==nil){
        mTimer = [NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(sendData) userInfo:nil repeats:YES];
        [mTimer fire];
    }
}

- (void) sendData{
    double lat = newLocation.coordinate.latitude;
    double lon = newLocation.coordinate.longitude;
    float rotate = [CalcuBusDriection directionRotate:lat longitude:lon];
    NSString * busStatus = [NSString stringWithFormat:@"{\"group\":\"driver\",\"action\":\"report_status\",\"id\":\"%@\",\"bus_type\":\"%@\",\"latitude\":\"%f\",\"longitude\":\"%f\",\"rotate\":\"%f\",\"direction\":\"%ld\",\"seat\":\"%ld\"}",self.driver.number,@"0",lat,lon,rotate,(long)appDelegate.direction,(long)set];
    [socketManager sendDataToServer:busStatus];
}

- (IBAction)add:(id)sender {
    set = 0;
    [self.peopleNumber setText:@"有空"];
}

- (IBAction)reduce:(id)sender {
    set = 1;
    [self.peopleNumber setText:@"满员"];
}

@end
